Certified Java Programmer Mock Exam


Question 1


class A {
  static boolean m1(String s, boolean b) {
    System.out.print(s + (b ? "T" : "F"));
    return b;
  }
  public static void main(String[] args) {
    m1("A",m1("B",false) || m1("C",true) || m1("D",false));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: ATBFCT
b. Prints: ATBFCTDF
c. Prints: BFCTAT
d. Prints: BTCTDFAT
e. Runtime error
f. Compiler error
g. None of the above

Question 2


class A {
  public static void main(String[] args) {
    final short s1 = 1; // 1
    final char c1 = 1;  // 2
    byte b1 = s1;       // 3
    byte b2 = c1;       // 4
    byte b3 = 1;        // 5
    byte b4 = 1L;       // 6
    byte b5 = 1.0;      // 7
    byte b6 = 1.0d;     // 8
  }
}

What is the result of attempting to compile the program?

a. Compiler error at 1
b. Compiler error at 2
c. Compiler error at 3
d. Compiler error at 4
e. Compiler error at 5
f. Compiler error at 6
g. Compiler error at 7
h. Compiler error at 8
i. Runtime error.
j. None of the Above

Question 3


class V {
  public static void main (String[] args) {
    float a = Float.POSITIVE_INFINITY;
    double b = Double.POSITIVE_INFINITY;
    double c = Double.NaN;
    System.out.print((a == b)+","+(c == c)+","+(c != c));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: false,false,false
b. Prints: false,false,true
c. Prints: false,true,false
d. Prints: false,true,true
e. Prints: true,false,false
f. Prints: true,false,true
g. Prints: true,true,false
h. Prints: true,true,true
i. Runtime error
j. Compiler error
k. None of the above

Question 4


class V {
  public static void main (String[] args) {
    System.out.print(Float.POSITIVE_INFINITY % 2 + ",");
    System.out.print(Float.NEGATIVE_INFINITY % 2 + ",");
    System.out.print(2 % Float.NEGATIVE_INFINITY);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: NaN,NaN,NaN
b. Prints: NaN,NaN,2.0
c. Prints: NaN,NaN,2
d. Prints: 2.0,2.0,2.0
e. Prints: 2,2,2
f. Runtime error
g. Compiler error
h. None of the above

Question 5


1.  interface I1 {}
2.  interface I2 {}
3.  class Base implements I1 {}
4.  class Sub extends Base implements I2 {}
5.  class Silver {
6.    public static void main(String []args) {
7.      Base[] base = {new Base()};
8.      Sub sub[] = new Sub[1];
9.      Object obj = base;
10.     sub = (Sub[])obj;
11.     I1 []i1 = (I1[])obj;
12.   }
13. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 8.
b. Runtime error at line 8.
c. Compiler error at line 9.
d. Runtime error at line 9.
e. Compiler error at line 10.
f. Runtime error at line 10.
g. Compiler error at line 11.
h. Runtime error at line 11.
i. Compiles and runs without error.

Question 6


class Q {
  static int m(int i) {
    System.out.print(i + ", ");
    return i;
  }

  public static void main(String s[]) {
    int i = 1;
    m(m(++i) + m(i++) + m(-i) + m(i++));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 1, 2, 3, 4, 10,
b. Prints: 1, 2, -3, 4, 4,
c. Prints: 2, 2, -3, -3, -2,
d. Prints: 2, 2, -3, 3, 4,
e. Prints: 2, 3, -3, -2, 0,
f. Prints: 2, 3, -3, 4, 6,
g. Prints: 2, 3, 4, 5, 14,
h. Runtime error
i. Compiler error
j. None of the above

Question 7


 1. interface I1 {}
 2. interface I2 {}
 3. class Base implements I1 {}
 4. class Sub extends Base implements I2 {}
 5.
 6. class Orange {
 7.   public static void main(String args[]) {
 8.     Base base = new Base();
 9.     I1 i1 = base;
10.     Sub sub = (Sub)base;
11.   }
12. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 9.
b. Runtime error at line 9.
c. Compiler error at line 10.
d. Runtime error at line 10.
e. Compiles and runs without error.

Question 8


1. class Maroon {
2.   public static void main (String[] args) {
3.     int i=1;
4.     short s = 1;
5.     long l=1,m=2;
6.     i = l + i;
7.     l = s + i;
8.   }
9. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 4.
b. Runtime error at line 4.
c. Compiler error at line 5.
d. Runtime error at line 5.
e. Compiler error at line 6.
f. Runtime error at line 6.
g. Compiler error at line 7.
h. Runtime error at line 7.
i. Compiles and runs without error.

Question 9


class Sienna {
  static double a;
  static float b;
  static int c;
  static char d;
  public static void main(String[] args) {

    a = b = c = d = 'a';
    System.out.println(a+b+c+d == 4 * 'a');
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: true
b. Prints: false
c. Compiler error.
d. Run time error.
e. None of the above.

Question 10


class A {}
class B extends A {}
class C extends B {
  static void m(A x, A y) {System.out.print("AA");}
  static void m(A x, B y) {System.out.print("AB");}
  static void m(B x, A y) {System.out.print("BA");}
  static void m(B x, B y) {System.out.print("BB");}
  public static void main(String[] args) {
    C c = new C();
    m(c, c);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: AA
b. Prints: AB
c. Prints: BA
d. Prints: BB
e. Compiler error.
f. Runtime error.
g. None of the Above

Question 11


class U {
  public static void main(String args[]) {
    int a = 1;
    a += ++a + a++;
    System.out.print(a);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 3
b. Prints: 4
c. Prints: 5
d. Prints: 6
e. Prints: 7
f. Runtime error
g. Compiler error
h. None of the above

Question 12


class C {
  static int m(int i) {
    System.out.print(i + ", ");
    return i;
  }

  public static void main(String s[]) {
    int i = 1;
    m(m(++i) - m(i++) + m(-i) * m(~i));
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: 2, 2, -3, -4, 8,
b. Prints: 2, 2, -3, -4, 12,
c. Prints: 2, 3, -3, -4, 7,
d. Prints: 1, 1, 1, 1, 0,
e. Prints: 2, 2, -2, -2, 4,
f. Prints: 2, 3, -2, -2, 3,
g. Prints: -1, -2, 2, 2, 0,
h. Runtime error
i. Compiler error
j. None of the above

Question 13


 1. interface I1 {}
 2. interface I2 {}
 3. class Base implements I1 {}
 4. class Sub extends Base implements I2 {}
 5. class Yellow {
 6.   public static void main(String args[]) {
 7.     Base base = new Sub();
 8.     I1 i1 = base;
 9.     Sub sub = (Sub)base;
10.     I2 i2 = (Sub)base;
11.   }
12. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 7.
b. Runtime error at line 7.
c. Compiler error at line 8.
d. Runtime error at line 8.
e. Compiler error at line 9.
f. Runtime error at line 9.
g. Compiler error at line 10.
h. Runtime error at line 10.
i. Compiles and runs without error.

Question 14


class A {}
class B {
  static void m(Object x) {System.out.print("Object");}
  static void m(String x) {System.out.print("String");}
  public static void main(String[] args) {
    m(null);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: Object
b. Prints: String
c. Compiler error.
d. Runtime error.
e. None of the Above

Question 15


1.  class White {
2.    public static void main(String args[]) {
3.      int[] i = {1,2,3,4,5};
4.      long[] l1 = new long[5];
5.      long []l2 = l1;
6.      long l3[] = (long[])i;
7.      long l4[] = new long[5];
8.      l4[1] = i[1];
9.    }
10. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 3.
b. Runtime error at line 3.
c. Compiler error at line 4.
d. Runtime error at line 4.
e. Compiler error at line 5.
f. Runtime error at line 5.
g. Compiler error at line 6.
h. Runtime error at line 6.
i. Compiler error at line 7.
j. Runtime error at line 7.
k. Compiler error at line 8.
l. Runtime error at line 8.
m. Compiles and runs without error.

Question 16


1. class Black {
2.   public static void main(String args[]) {
3.     int[] i = {1,2,3,4,5};
4.     long[] l = new long[5];
5.     for (int j=0;j < l.length(); j++) {
6.       l[j] = i[j];
7.     }
8.   }
9. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 3.
b. Runtime error at line 3.
c. Compiler error at line 4.
d. Runtime error at line 4.
e. Compiler error at line 5.
f. Runtime error at line 5.
g. Compiler error at line 6.
h. Runtime error at line 6.
i. Compiles and runs without error.

Question 17


class A {}
class B {
  static void m(Object x) {System.out.print("Object");}
  static void m(String x) {System.out.print("String");}
  static void m(A x) {System.out.print("A");}
  public static void main(String[] args) {
    m(null);
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: Object
b. Prints: String
c. Compiler error.
d. Runtime error.
e. None of the Above

Question 18


1.  class Amber {
2.    public static void main(String[] args) {
3.      int[][] a = {{1,2},{0,1,2},{-1,0,2}};
4.      Object[] obj = (Object[])a.clone();
5.      for(int i = 0;i<obj.length; i++) {
6.        int[] ia = (int[])obj[i];
7.        System.out.print(ia[i]);
8.      }
9.    }
10. }

What is the result of attempting to compile and run the above program?

a. Compiler error at line 3.
b. Compiler error at line 4.
c. Compiler error at line 5.
d. Compiler error at line 6.
e. Compiler error at line 7.
f. Run time error.
g. None of the above.


Copyright © 2002, Dan Chisholm
All rights reserved.
1